package ru.papergames.web.controllers;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import ru.common.hibernate.HibernateActionExecutor;
import ru.papergames.common.form.PcPlayerForm;
import ru.papergames.web.cache.PlayersManager;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author Alexander Troshanin
 */
@Controller
public class BattleController extends AuthorizedController {

    private static final Random rnd = new Random();

    private final HibernateActionExecutor hiber;

    public BattleController(@Qualifier("paperGamesHibernate") HibernateActionExecutor hiber) {
        this.hiber = hiber;
    }

    @RequestMapping(value = "/add-player-in-base-fighters-pool.html")
    public ModelAndView addPlayerInBaseFightersPool(HttpServletRequest request) {
        final PcPlayerForm player = getPcPlayerForm(request);

        ModelAndView mv = new ModelAndView("papergames/addPlayerInBaseFightersPool");
        List<String> errors = new ArrayList<String>();
        boolean result = PlayersManager.getInstance().addPlayerInBaseFightersPool(player, errors);
        mv.addObject("result", result);
        // TODO: send timeout - max 30sec to wait
        mv.addObject("errors", errors);
        return mv;
    }

}
